<template>
  <!-- <el-dialog title="分配角色" :visible.sync="roleVisible"  /> -->
  <el-dialog v-show="show" title="分配角色" :visible="roleVisible" @update:visible="$emit('update:roleVisible',$event)">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="save">确定</el-button>
      <el-button size="small" @click="$emit('update:roleVisible',false)">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleVisible: {
      type: Boolean,
      required: true
    },
    id: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      list: [],
      checkList: [], // 选中的所有的选项组成的一个列表
      show: false
    }
  },
  async created() {
    const res = await getRoleList()
    console.log(res)
    this.list = res.rows

    const res1 = await getUserDetailById(this.id)
    this.checkList = res1.roleIds
    this.show = true
  },
  methods: {
    async save() {
      await assignRoles({ id: this.id, roleIds: this.checkList })
      this.$message.success('分配成功')
      this.$emit('update:roleVisible', false)
    }
  }
}
</script>

<style scoped lang='scss'></style>
